﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;

namespace StyrConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            ILog Log = LogManager.GetLogger("Logger");

            var Port = 8081;

            IPEndPoint Sender;
            UdpClient Client;
            byte[] Buffer;
            string LogLine;

            Log.Info("ConsoleReady");
            try
            {
                Sender = new IPEndPoint(IPAddress.Any, 0);
                Client = new UdpClient(Port);

                while (true)
                {
                    Buffer = Client.Receive(ref Sender);
                    LogLine = Encoding.Default.GetString(Buffer);

                    if (LogLine.IndexOf("{INFO}") >= 0)
                        Log.Info(LogLine.Replace("{INFO}", "[I] "));
                    else if (LogLine.IndexOf("{DEBUG}") >= 0)
                        Log.Debug(LogLine.Replace("{DEBUG}", "[D] "));
                    else if (LogLine.IndexOf("{ERROR}") >= 0)
                        Log.Error(LogLine.Replace("{ERROR}", "[E] "));
                    else if (LogLine.IndexOf("{WARN}") >= 0)
                        Log.Warn(LogLine.Replace("{WARN}", "[W] "));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadLine();
            }
        }
    }
}
